package screen_calibration;
/*************************************************
 * File:	CompColor.java
 * Description:	Colored rectangle component
 *
 * Author: Gene Vishnevsky  Oct. 15, 1997
*************************************************/

import java.awt.*;

/**
* This class produces a component that represents RGB color
* as a colored rectangle.
* @see UpdatableComponentImpl
*/
public class CompColor extends UpdatableComponentImpl {

	private RGBCoord rgbc;
	
	private static final String WARNING = "Invalid RGB...";
	
	private static final Font nef = new Font( "Helvetica", Font.PLAIN, 12 );

	/**
	* Constructs colored rectangle component with argument.
	* @param rgbc reference to RGB coordinates.
	* @see RGBCoord
	*/
	public CompColor( RGBCoord rgbc ) {

		this.rgbc = rgbc;

		resize( minimumSize() );
	}

	/**
	* Self-update method. It is called when the component has
	* to be updated.
	*/
	public void update() {

		if( rgbc.isValid() ) {

			setBackground( new Color( rgbc.getRed(), rgbc.getGreen(),
				rgbc.getBlue() ) );
		}
		else {
			Graphics g = getGraphics();

			setBackground( Color.white );

			Font old = g.getFont();

			g.setFont( nef );
			int w = g.getFontMetrics().stringWidth( WARNING );
			g.setColor( Color.red );
			g.drawString( WARNING, (size().width - w) / 2, size().height / 2 - 1 );

			g.setFont( old );

			g.dispose();
		}

		repaint();
	}


	/**
	* Overrides the default minimumSize() method.
	* @return new Dimension
	*/
	public Dimension minimumSize() {
		return new Dimension( 80, 40 );
	}

	/**
	* Overrides the default preferredSize() method.
	* @return new Dimension
	*/
	public Dimension preferredSize() {
		return minimumSize();
	}

	/**
	* Overrides the default insets() method.
	* @return new Insets
	*/
	public Insets insets() {
		return new Insets( 4, 4, 4, 4 );
	}


	/**
	* Overrides the default update(Graphics) method
	* in order not to clear screen to avoid flicker.
	*/
	public void update( Graphics g ) {
		paint( g );
	}
}
